/*
Copyright 2011 Michael Mallon

This file is part of SQAT.

SQAT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

SQAT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with SQAT.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Coefficient.h"
#include <iostream>
#include <cmath>
#include <limits>

using namespace std;

Coefficient::Coefficient() {
	number = 0;
	power = 0;
	variable = string("");

}

Coefficient::~Coefficient() {
	// TODO Auto-generated destructor stub
}

Coefficient & Coefficient::operator=(Coefficient c)
{
	if (this == &c) {
		return *this;
	}
	number = c.getNumber();
	power = c.getPower();
	variable = c.getVariable();
	return *this;
}

Coefficient Coefficient::operator /(Coefficient c)
{
	Coefficient result = *this;
	result /= c;
	return result;
}



Coefficient Coefficient::operator +(Coefficient c)
{
	Coefficient result = *this;
	result += c;
	return result;
}



Coefficient Coefficient::operator *(Coefficient c)
{
	Coefficient result = *this;
	result *= c;
	return result;
}



Coefficient Coefficient::operator -(Coefficient c)
{
	Coefficient result = *this;
	result -= c;
	return result;
}

double Coefficient::getNumber() const
{
    return number;
}

double Coefficient::getPower() const
{
    return power;
}

void Coefficient::setNumber(double number)
{
    this->number = number;
}

void Coefficient::setPower(double power)
{
    this->power = power;
}

Coefficient Coefficient::operator *=(Coefficient c)
{
	if (!(*this).likeTerms(c)) {
		cerr << "Cannot simplify different variables"<<endl;
		throw;
	}
	number *= c.getNumber();
	power += c.getPower();
	return *this;
}

Coefficient Coefficient::operator /=(Coefficient c)
{
	if (!(*this).likeTerms(c)) {
		cerr << "Cannot simplify different variables"<<endl;
		throw;
	}
	number /= c.getNumber();
	power -= c.getPower();
	return *this;
}

Coefficient Coefficient::operator +=(Coefficient c)
{
	if (!(*this).likeTerms(c)) {
		cerr << "Cannot simplify different variables"<<endl;
		throw;
	} else if (abs(power - c.power) > numeric_limits<double>::epsilon() ) {
		cerr << "Cannot simplify different powers of a variable"<<endl;
		throw;
	}
	number += c.getNumber();
	return *this;
}

string Coefficient::getVariable() const
{
	return variable;
}

void Coefficient::setVariable(string v)
{
	variable = v;
}

Coefficient Coefficient::operator -=(Coefficient c)
{
	if (!(*this).likeTerms(c)) {
			cerr << "Cannot simplify different variables"<<endl;
			throw;
		} else if (abs(power - c.power) > numeric_limits<double>::epsilon() ) {
			cerr << "Cannot simplify different powers of a variable"<<endl;
			throw;
		}
		number -= c.getNumber();
		return *this;
}

bool Coefficient::likeTerms(Coefficient c)
{
	return (variable.compare(c.getVariable())==0);
}
